package com.ldl.origami.controller.ranking;

import com.ldl.origami.common.ErrorModelAndView;
import com.ldl.origami.domain.Tutorial;
import com.ldl.origami.domain.TutorialData;
import com.ldl.origami.domain.User;
import com.ldl.origami.origamiService.RankingService;
import com.ldl.origami.origamiService.UserService;
import com.ldl.origami.util.Constants;
import com.ldl.origami.util.InitServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 功能描述:排行
 * 作者: LDL
 * 创建时间: 2014/9/4 16:51
 */
@Controller
public class RankingController {


    @Autowired
    private RankingService rankingService;

    @Autowired
    private UserService userService;

    /**
     * 排行页面
     * @param request
     * @return
     */
    @RequestMapping("/ranking")
    public ModelAndView ranking(HttpServletRequest request){
        HttpSession session = request.getSession();
        String userName = (String) session.getAttribute(Constants.SESSION_USERNAME);
        ModelAndView modelAndView = new ModelAndView();
        TutorialData tutorialData = new TutorialData();
        User user = null;
        List<Tutorial> hotTutorials = null;
        List<Tutorial> newTutorials = null;
        List<String> users = null;
        try {
            user = userService.getUserByUserName(userName);
            hotTutorials = rankingService.getHotList(1);
            newTutorials = rankingService.getNewList(1);
            users = rankingService.getTop10Users();
        } catch (Exception e) {
            return new ErrorModelAndView(user);
        }
        tutorialData.setHotTutorials(hotTutorials);
        tutorialData.setNewTutorials(newTutorials);
        /**
         * 判断是否需要显示更多
         */
        if(hotTutorials!=null&&hotTutorials.size() < InitServlet.hotlistLimit){
            tutorialData.setHotlist_requires_pagination(false);
        }
        if (newTutorials != null && newTutorials.size() < InitServlet.newlistLimit) {
            tutorialData.setNewlist_requires_pagination(false);
        }
        modelAndView.addObject("user", user);
        modelAndView.addObject("users",users);
        modelAndView.addObject("tutorialData", tutorialData);
        modelAndView.setViewName("ranking/ranking");
        return modelAndView;
    }

    /**
     * 读取更多热门排行
     * @param request
     * @return
     */
    @RequestMapping("/showMoreHot")
    @ResponseBody
    public TutorialData showMoreHot(HttpServletRequest request){

        int page = Integer.parseInt(request.getParameter("page"));
        TutorialData tutorialData = new TutorialData();
        User user = null;
        List<Tutorial> hotTutorials = null;
        try {
            hotTutorials = rankingService.getHotList(page);
        } catch (Exception e) {
        }
        if ((hotTutorials != null && hotTutorials.size() < InitServlet.hotlistLimit)||page==InitServlet.hotlistPage){
            tutorialData.setHotlist_requires_pagination(false);
        }
        tutorialData.setHotTutorials(hotTutorials);
        return tutorialData;
    }


    /**
     * 读取更多最新发布
     *
     * @param request
     * @return
     */
    @RequestMapping("/showMoreNew")
    @ResponseBody
    public TutorialData showMoreNew(HttpServletRequest request) {

        int page = Integer.parseInt(request.getParameter("page"));
        TutorialData tutorialData = new TutorialData();
        User user = null;
        List<Tutorial> newTutorials = null;
        try {
            newTutorials = rankingService.getNewList(page);
        } catch (Exception e) {
        }
        if ((newTutorials != null && newTutorials.size() < InitServlet.newlistLimit) || page == InitServlet.newlistPage) {
            tutorialData.setNewlist_requires_pagination(false);
        }
        tutorialData.setNewTutorials(newTutorials);
        return tutorialData;
    }
}
